package com.example.exercise.Utils;

import android.widget.Toast;

import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class GetNetWorkTime {


    public static int getNetTime() {

        String device_time = "";
        final String[] format = new String[1];
        int result = 0;


        URL url = null;//取得资源对象
        try {
            //url = new URL("http://www.baidu.com");
            url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
            //url = new URL("http://www.bjtime.cn");
            URLConnection uc = url.openConnection();//生成连接对象
            uc.connect(); //发出连接
            long ld = uc.getDate(); //取得网站日期时间
            DateFormat formatter = new SimpleDateFormat("dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(ld);
            format[0] = formatter.format(calendar.getTime());
            Date date = new Date(System.currentTimeMillis());
            device_time = formatter.format(date);
            if (Integer.parseInt(device_time) != Integer.parseInt(format[0])){
                result = 0;
            }else {
                result = 1;
            }

        } catch (Exception e) {
            e.printStackTrace();

        }

        return result;


    }

    public static String getNetHour() {

        final String[] format = new String[1];



        URL url = null;//取得资源对象
        try {
            //url = new URL("http://www.baidu.com");
            url = new URL("http://www.ntsc.ac.cn");//中国科学院国家授时中心
            //url = new URL("http://www.bjtime.cn");
            URLConnection uc = url.openConnection();//生成连接对象
            uc.connect(); //发出连接
            long ld = uc.getDate(); //取得网站日期时间
            DateFormat formatter = new SimpleDateFormat("kk");
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(ld);
            format[0] = formatter.format(calendar.getTime());

        } catch (Exception e) {
            e.printStackTrace();

        }

       return format[0];


    }
}
